//
//  THIconTableViewCell.m
//  YJB
//
//  Created by 中电大维 on 2017/1/18.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THIconTableViewCell.h"
#import "THFEMyPublishDetailModel.h"

NSString *const THIconCellID = @"THIconCellID";
@implementation THIconTableViewCell
{
    UIView *_line;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self addSubview:self.iconImage];
        [self addSubview:self.username];
        [self addSubview:self.time];
        [self addSubview:self.state];
        [self addSubview:self.totalgold];
        
        [self setFrame];
//        [self test];
    }
    return self;
}

- (void)test{
    self.username.text = @"18676727214";
    self.time.text = @"2个小时前";
    self.state.text = @"共 20 金币";
    self.totalgold.text = @"原 25 金币";
}

- (void)setModel:(THFEMyPublishDetailModel *)model
{
    NSString *urlString = model.surf_icon;
    if (![urlString hasPrefix:@"http"] && urlString)
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];
    
    self.username.text = [NSString stringWithFormat:@"%@",model.username];
    self.state.text = [NSString stringWithFormat:@"共 %@ 金币",model.totalgold];
    self.time.text = [model.create_time stringWithDateFormat:@"yyyy.MM.dd hh:mm"];
    
    if ([model.benefe_val floatValue]) {
        //总共金币
        NSString *total = [NSString stringWithFormat:@"原 %.2f 金币",[model.totalgold floatValue]+[model.benefe_val floatValue]];
        self.state.text = [NSString stringWithFormat:@"付 %@ 金币",model.totalgold];
        self.totalgold.text = total;
        self.totalgold.hidden = NO;
    }else{
        self.totalgold.hidden = YES;
        self.state.text = [NSString stringWithFormat:@"共 %@ 金币",model.totalgold];
    }
    
    CGFloat stateWidth = [NSString widthForString:_state.text fontSize:15 andHeight:15]+2;
    CGFloat totalWidth = [NSString widthForString:_totalgold.text fontSize:12 andHeight:15]+2;
    [_state mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(stateWidth);
    }];
    [_totalgold mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(totalWidth);
    }];
}



- (void)setFrame{
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.left.equalTo(self.mas_left).offset(15);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.width.equalTo(_iconImage.mas_height);
    }];
    [self.username mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.iconImage.mas_right).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(40);
    }];
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).mas_offset(10);
        make.right.equalTo(self.mas_right).offset(-12);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(15);
    }];
    [self.state mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-12);
        make.height.mas_equalTo(15);
        make.width.mas_offset(100);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    
    [self.totalgold mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.state.mas_left).offset(-6);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.height.mas_equalTo(15);
        make.width.mas_offset(100);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(_totalgold.center);
        make.width.equalTo(_totalgold.mas_width);
        make.height.mas_equalTo(0.5);
    }];
}

-(UIImageView *)iconImage
{
    if (!_iconImage) {
        _iconImage = [[UIImageView alloc] init];
        _iconImage.image = Image(@"th_cpc_default_normal");
    }
    return _iconImage;
}

-(UILabel *)username
{
    if (_username == nil)
    {
        _username = [[UILabel alloc]init];
        _username.backgroundColor = [UIColor clearColor];
        _username.font = [UIFont systemFontOfSize:16];
        _username.numberOfLines = 2;
        _username.textColor = kUIColorFromRGB(0x333333);
    }
    return _username;
}

-(UILabel *)time
{
    if (_time == nil)
    {
        _time = [[UILabel alloc]init];
        _time.backgroundColor = [UIColor clearColor];
        _time.font = [UIFont systemFontOfSize:14];
        _time.numberOfLines = 0;
        _time.textColor = kUIColorFromRGB(0x666666);
        _time.textAlignment = NSTextAlignmentRight;
    }
    return _time;
}

-(UILabel *)state
{
    if (_state == nil)
    {
        _state = [[UILabel alloc]init];
        _state.font = FONT(15);
        _state.textColor = kUIColorFromRGB(0xff783c);
        _state.textAlignment = NSTextAlignmentRight;
    }
    return _state;
}

-(UILabel *)totalgold
{
    if (_totalgold == nil)
    {
        _totalgold = [[UILabel alloc]init];
        _totalgold.font = FONT(12);
        _totalgold.textColor = kUIColorFromRGB(0x666666);
        _totalgold.textAlignment = NSTextAlignmentRight;
        
        _line = [[UIView alloc] init];
        _line.backgroundColor = kUIColorFromRGB(0x666666);
        [_totalgold addSubview:_line];
        _totalgold.hidden = YES;
    }
    return _totalgold;
}

@end
